package com.my.test.dao;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.geo.Point;
import org.springframework.stereotype.Repository;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import com.mongodb.AggregationOptions;
//import com.mongodb.AggregationOptions;
import com.mongodb.BasicDBObject;
import com.mongodb.Cursor;
import com.mongodb.DBObject;
//import com.mongodb.Cursor;

@Repository
public class MongoGeoDaoImpl implements MongoGeoDao{

	 @Autowired
	 private MongoTemplate mongoTemplate;
	 
	@Override
	public List<DBObject> geoNear(String collection, DBObject query, Point point, int limit, long maxDistance) {
		if(query==null)  
            query = new BasicDBObject();  
   
        List<DBObject> pipeLine = new ArrayList<>();  
        BasicDBObject aggregate = new BasicDBObject("$geoNear",  
                new BasicDBObject("near",new BasicDBObject("loc",new double[]{118.783799, 31.979234}))  
                        .append("distanceField","dist.calculated")  
//                        .append("query", new BasicDBObject())  
//                        .append("num", 5)
//                        .append("maxDistance", 5000)
                        .append("spherical",true)
                );
        pipeLine.add(aggregate);
        System.out.println("aggregate="+aggregate);
        Cursor cursor=mongoTemplate.getCollection(collection).aggregate(pipeLine, AggregationOptions.builder().build());  
        List<DBObject> list = new LinkedList<>();  
        while (cursor.hasNext()) {  
            list.add(cursor.next());  
        }  
        return list;
	}

	@Override
	public List<DBObject> withinCircle(String collection, String locationField, Point center, long radius,
			DBObject fields, DBObject query, int limit) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<DBObject> nearSphere(String collection, String locationField, Point center, long minDistance,
			long maxDistance, DBObject query, DBObject fields, int limit) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<DBObject> withinPolygon(String collection, String locationField, List<double[]> polygon,
			DBObject fields, DBObject query, int limit) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<DBObject> withinMultiPolygon(String collection, String locationField, List<List<double[]>> polygons,
			DBObject fields, DBObject query, int limit) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<DBObject> withinBox(String collection, String locationField, Point bottomLeft, Point upperRight,
			DBObject fields, DBObject query, int limit) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<DBObject> getNear(Query query) {
		// TODO Auto-generated method stub
		return null;
	}

}
